Imports DAL
Imports MyClasses
Imports C1.Win.C1FlexGrid
Imports System.Configuration
Public Class frmStock

    Public _strStockType As String = "SMR"
    Dim chkflag As Boolean

    Private Sub frmStock_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.MdiParent = Nothing
    End Sub

    Private Sub frmStock_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Try
            If e.KeyCode = Keys.F1 Then
                'Rauhl_ITS@20073012 : Add following code display help
                e.SuppressKeyPress = True
                Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Stock Summary")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmStock_KeyUp")
        End Try
    End Sub

    Private Sub frmStock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ''Dim user As String = String.Empty
            'Dim dtrole As DataTable = Nothing
            Call BCommon.SetFormStandards(Me)
            LoadList()
            GridSettings()

            ''user = CCommon.GetUserID()
            ''dtrole = BStock.GetRoleId(user)
            ''If dtrole.Rows.Count <> 0 Then
            ''    If dtrole.Rows(0).Item(0) <> 1 Then
            ''        rdbtnPriceAdj.Enabled = False
            ''        rdbtnStockAdj.Enabled = False
            ''    End If
            ''End If
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                Call AssignPrivilege()   'Divya@20/03/2008 : Added regarding assignment of Roles & Privilege
            Else
                lstCompany.Enabled = True
                lstVendor.Enabled = True
                lstLocation.Enabled = True
                chkAvailable.Enabled = True
                txtItemName.Enabled = True
                chkAll.Enabled = True
                chkExp.Enabled = True
                rdnExpItems.Enabled = True
                rdbtnSummary.Enabled = True
                fgResults.Enabled = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmStock_Load")
        End Try
    End Sub

    'Divya@20/03/2008 : Added regarding assignment of Roles & Privilege
    Public Sub AssignPrivilege()
        Dim intCount As Integer
        Dim dtPriv As New DataTable
        Try
            Me.rdbtnPriceAdj.Enabled = False
            Me.rdbtnStockAdj.Enabled = False

            dtPriv = BRole.GetRolePriv(ConfigurationManager.AppSettings("UserID"))
            If dtPriv.Rows.Count > 0 Then
                For intCount = 0 To dtPriv.Rows.Count - 1
                    Select Case dtPriv.Rows(intCount).Item("PrID")
                        Case 1
                            Me.rdbtnPriceAdj.Enabled = True               'Price Adjustment
                        Case 2
                            Me.rdbtnStockAdj.Enabled = True               'Stock Adjustment
                    End Select
                Next
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "AssignPrivilege")
        End Try
    End Sub

    Sub LoadList()
        Try
            Dim tbl As DataTable
            tbl = BStock.GetAllLocation
            lstLocation.DataSource = tbl
            lstLocation.ValueMember = tbl.Columns(0).ToString()
            lstLocation.DisplayMember = tbl.Columns(1).ToString()

            tbl = BStock.GetAllVendor
            lstVendor.DataSource = tbl
            lstVendor.ValueMember = tbl.Columns(0).ToString()
            lstVendor.DisplayMember = tbl.Columns(1).ToString()

            tbl = BStock.GetAllManufacturer
            lstCompany.DataSource = tbl
            lstCompany.ValueMember = tbl.Columns(0).ToString()
            lstCompany.DisplayMember = tbl.Columns(1).ToString()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "LoadList")
        End Try
    End Sub

    Private Sub lstVendor_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstVendor.ItemCheck
        Try
            'Rahul_ITS@20080215:
            If e.Index = 0 AndAlso chkflag = False Then
                If e.NewValue = CheckState.Checked Then
                    selectAll(sender)
                    lstVendor.SelectedIndex = e.Index
                ElseIf e.NewValue = CheckState.Unchecked Then
                    ClearAll(sender)
                    lstVendor.SelectedIndex = e.Index
                End If
            Else
                If chkflag = False Then
                    chkflag = True
                    If e.NewValue = CheckState.Unchecked Then
                        'UNCHECKED the First CheckBox(All)
                        lstVendor.SetItemChecked(0, False)  'Modified BY:-VINIT(2008/02/16)
                    ElseIf e.NewValue = CheckState.Checked Then
                        'CHECKED the First CheckBox(All)
                        Call SelectChkAll(sender)   'Modified BY:-VINIT(2008/02/16)
                    End If
                End If
            End If
            chkflag = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "lstVendor_ItemCheck")
        End Try
    End Sub

    Sub selectAll(ByVal basectl As CheckedListBox)
        Try
            Dim i As Integer
            For i = 1 To basectl.Items.Count - 1
                basectl.SetItemCheckState(i, CheckState.Checked)
            Next
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "selectAll")
        End Try
    End Sub

    Private Sub lstLocation_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstLocation.ItemCheck
        Try 'Rahul_ITS@20080215:
            If e.Index = 0 AndAlso chkflag = False Then
                If e.NewValue = CheckState.Checked Then
                    selectAll(sender)
                    lstLocation.SelectedIndex = e.Index
                ElseIf e.NewValue = CheckState.Unchecked Then
                    ClearAll(sender)
                    lstLocation.SelectedIndex = e.Index
                End If
            Else
                If chkflag = False Then
                    chkflag = True
                    If e.NewValue = CheckState.Unchecked Then
                        'UNCHECKED the First CheckBox(All)
                        lstLocation.SetItemChecked(0, False)  'Modified BY:-VINIT(2008/02/16)
                    ElseIf e.NewValue = CheckState.Checked Then
                        'CHECKED the First CheckBox(All)
                        Call SelectChkAll(sender) 'Modified BY:-VINIT(2008/02/16)
                    End If
                End If
            End If
            chkflag = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "lstLocation_ItemCheck")
        End Try
    End Sub

    'Written BY:-VINIT(2008/02/16)
    Private Sub SelectChkAll(ByVal chkObj As CheckedListBox)
        Try
            If ((chkObj.Items.Count - 1) - chkObj.CheckedItems.Count) = 1 Then
                chkObj.SetItemCheckState(0, CheckState.Checked)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "selectAll")
        End Try
    End Sub


    Sub ClearAll(ByVal basectl As CheckedListBox)
        Try
            Dim i As Integer
            For i = 1 To basectl.Items.Count - 1
                basectl.SetItemCheckState(i, CheckState.Unchecked)
            Next
            basectl.SelectedIndex = -1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearAll")
        End Try
    End Sub

    Private Sub lstCompany_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstCompany.ItemCheck        
        Try 'Rahul_ITS@20080215:
            If e.Index = 0 AndAlso chkflag = False Then
                If e.NewValue = CheckState.Checked Then
                    selectAll(sender)
                    lstCompany.SelectedIndex = e.Index
                ElseIf e.NewValue = CheckState.Unchecked Then
                    ClearAll(sender)
                    lstCompany.SelectedIndex = e.Index
                End If
            Else
                If chkflag = False Then
                    chkflag = True
                    If e.NewValue = CheckState.Unchecked Then
                        'UNCHECKED the First CheckBox(All)
                        lstCompany.SetItemChecked(0, False)  'Modified BY:-VINIT(2008/02/16)
                    ElseIf e.NewValue = CheckState.Checked Then
                        'CHECKED the First CheckBox(All)
                        Call SelectChkAll(sender)   'Modified BY:-VINIT(2008/02/16)
                    End If
                End If
            End If
            chkflag = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "lstCompany_ItemCheck")
        End Try
    End Sub

    Public Function SetData() As CStockQuery
        Try
            Dim stockqry As New CStockQuery
            stockqry.Company = BCommon.GetCheckedIdList(lstCompany)
            stockqry.Location = BCommon.GetCheckedIdList(lstLocation)
            stockqry.Vendor = BCommon.GetCheckedIdList(lstVendor)
            stockqry.AvailableOnly = chkAvailable.Checked

            If chkExp.Checked Then
                stockqry.Status = CStockQuery.StockStatus.NearExpiry
            ElseIf rdnExpItems.Checked Then
                stockqry.Status = CStockQuery.StockStatus.Expired
            ElseIf chkAll.Checked Then
                stockqry.Status = CStockQuery.StockStatus.All
            End If
            Return stockqry
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Return Nothing
        End Try
    End Function

    Sub GridSettings()
        Try
            ' set up grid layout/behavior
            With fgResults
                .AllowEditing = False
                .AllowSorting = AllowSortingEnum.None
                .AllowMerging = AllowMergingEnum.Nodes
                .SelectionMode = SelectionModeEnum.Cell
                .ExtendLastCol = True

                '.Cols(0).Width = .Cols.DefaultSize / 4
                .Tree.Style = TreeStyleFlags.Simple
                .Tree.Column = 2
                ' set up grid styles

                .Styles.Normal.Border.Style = BorderStyleEnum.None
                .Styles.Normal.Trimming = StringTrimming.EllipsisCharacter

                Dim s As CellStyle = .Styles(CellStyleEnum.GrandTotal)
                s.BackColor = Color.Black
                s.ForeColor = Color.White
                's = .Styles(CellStyleEnum.Subtotal0)
                s.BackColor = Color.Gold
                s.ForeColor = Color.Black
                s = .Styles(CellStyleEnum.Subtotal1)
                s.BackColor = Color.Khaki
                s.ForeColor = Color.Black
                s = .Styles(CellStyleEnum.Subtotal2)
                s.BackColor = Color.LightGoldenrodYellow

                SetCaption()
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "GridSettings")
        End Try
    End Sub

    Sub SetCaption()
        Try
            With fgResults
                'CAPTION 
                .Cols(1).Caption = "Stock Code"
                .Cols(2).Caption = "Code"
                .Cols(3).Caption = "Item Name"
                .Cols(4).Caption = "Batch No"
                .Cols(5).Caption = "Exp"
                .Cols(6).Caption = "Location"
                .Cols(7).Caption = "Stock"
                .Cols(8).Caption = "Price"
                .Cols(9).Caption = "Amount"
                .Cols(1).Visible = False
            End With
            'Rahul_ITS@20071115 :

            Select Case _strStockType
                Case "PRA"
                    With fgResults
                        .Cols.Count = 15
                        .Cols(10).Caption = "Cur. Margin"
                        .Cols(11).Caption = "Cur. Sale Price"
                        .Cols(12).Caption = "New Margin"
                        .Cols(13).Caption = "New Sale Price"
                        .Cols(14).Caption = "Item ID"
                        .Cols(3).Visible = False
                        .Cols(14).Visible = False
                    End With
                Case "STKA"
                    With fgResults
                        .Cols.Count = 12
                        .Cols(10).Caption = "New Stock"
                        .Cols(10).DataType = System.Type.GetType("System.Int32")
                        .Cols(11).Caption = "Item ID"
                        .Cols(11).Visible = False
                        .Cols(3).Visible = False
                    End With
            End Select
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetCaption")
        End Try
    End Sub

    Private Sub fgResults_AfterDataRefresh(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles fgResults.AfterDataRefresh
        Try
            Dim totalOn As Integer = fgResults.Cols("Amount").SafeIndex
            Dim caption As String = "Total for {0}"

            fgResults.Subtotal(AggregateEnum.Sum, 0, 2, 3, totalOn, caption)

            fgResults.Tree.Show(1)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgResults_AfterDataRefresh")
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            ''Rahul_ITS@20071119
            chkAll.Checked = True
            rdbtnSummary.Checked = True

            chkExp.Checked = False
            rdnExpItems.Checked = False
            chkAvailable.Checked = False
            rdbtnPriceAdj.Checked = False
            rdbtnStockAdj.Checked = False
            txtItemName.Text = ""

            ClearAll(lstCompany)
            ClearAll(lstLocation)
            ClearAll(lstVendor)

            With fgResults
                .DataSource = Nothing
                If .Rows.Count > 1 Then
                    .Clear(C1.Win.C1FlexGrid.ClearFlags.Content, 1, 0, .Rows.Count - 1, .Cols.Count - 1)
                    .Rows.RemoveRange(1, .Rows.Count - 1)
                    .Rows.Count = 20
                    .Refresh()
                End If
            End With
            LoadList()
            GridSettings()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
        End Try
    End Sub

    Private Sub btnSrch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click
        Dim IntCount As Integer
        Dim ItemLike As String
        Try
            fgResults.Refresh()
            If rdbtnPriceAdj.Checked = True Then
                _strStockType = "PRA"
            ElseIf rdbtnStockAdj.Checked = True Then
                _strStockType = "STKA"
            Else
                _strStockType = "SMR"
            End If
            ItemLike = Trim((txtItemName.Text).ToString())
            Dim DTB As DataTable = BStock.GetStockDetails(SetData, _strStockType, ItemLike)
            If DTB.Rows.Count <= 0 Then
                BCommon.ShowMsgError(10082)
                btnSrch.Focus()
            Else
                fgResults.DataSource = DTB
                fgResults.Refresh()
                SetCaption()
                For IntCount = 1 To fgResults.Rows.Count - 1
                    If fgResults.Rows(IntCount).IsNode = True Then
                        fgResults.Rows(IntCount).AllowEditing = False
                    End If
                Next
                Call Setcontrol()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSrch_Click")
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    'Rahul_ITS@20071115
    Private Sub Setcontrol()
        btnSave.Visible = False
        fgResults.AllowEditing = True
        Try
            With fgResults

                'If rdbtnSummary.Checked = True Or chkAll.Checked = True _
                'Or chkAvailable.Checked = True Or chkExp.Checked = True Then

                .Cols(0).AllowEditing = False
                .Cols(1).AllowEditing = False ' Stock ID
                .Cols(2).AllowEditing = False 'SeqItem ID
                .Cols(3).AllowEditing = False 'Item Name
                .Cols(4).AllowEditing = False
                .Cols(5).AllowEditing = False
                .Cols(6).AllowEditing = False
                .Cols(7).AllowEditing = False
                .Cols(8).AllowEditing = False
                .Cols(9).AllowEditing = False

                .Cols(1).Visible = False
                If _strStockType = "SMR" Then
                    .Cols(8).Format = ("0.00")
                    .Cols(9).Format = ("0.00")
                    .Cols(3).AllowEditing = False
                    .Cols(3).Width = .Width() / 3.5  'Item Name
                    .Cols(4).AllowEditing = False
                    .Cols(4).Width = .Width() / 10  'Batch NO
                End If
                If rdbtnPriceAdj.Checked = True Then
                    _strStockType = "PRA"
                    btnSave.Visible = True

                    .Cols(0).AllowEditing = False

                    .Cols(1).AllowEditing = False   ' Stock ID

                    .Cols(2).AllowEditing = False
                    .Cols(2).Width = .Width() / 13.5  'SeqItem ID

                    .Cols(3).AllowEditing = False
                    .Cols(3).Width = .Width() / 6  'Item Name

                    .Cols(4).AllowEditing = False
                    .Cols(4).Width = .Width() / 12  'Batch NO

                    .Cols(5).AllowEditing = False
                    .Cols(5).Width = .Width() / 18  'Exp

                    .Cols(6).AllowEditing = False
                    .Cols(6).Width = .Width() / 15 'Location Code

                    .Cols(7).AllowEditing = False
                    .Cols(7).Width = .Width() / 17 'Quantity

                    .Cols(8).AllowEditing = False
                    .Cols(8).Width = .Width() / 17 'Purchase Price
                    .Cols(8).Format = ("0.00")

                    .Cols(9).AllowEditing = False
                    .Cols(9).Width = .Width() / 9  'Amount
                    .Cols(9).Format = ("0.00")

                    .Cols(10).AllowEditing = False
                    .Cols(10).Width = .Width() / 12 'Current Margin
                    .Cols(10).Format = ("0.00")

                    .Cols(11).Width = .Width() / 8 'Current Sale Price
                    .Cols(11).AllowEditing = False
                    .Cols(11).Format = ("0.00")

                    .Cols(12).Width = .Width() / 8  'New Margin
                    .Cols(12).DataType = System.Type.GetType("System.Decimal")

                    .Cols(13).Width = .Width() / 14 'New Sale Price
                    .Cols(13).DataType = System.Type.GetType("System.Decimal")

                    .Cols(1).Visible = False
                    .Cols(14).Visible = False   'Item ID

                ElseIf rdbtnStockAdj.Checked = True Then
                    _strStockType = "STKA"
                    btnSave.Visible = True

                    .Cols(0).AllowEditing = False

                    .Cols(1).AllowEditing = False   'Stock ID

                    .Cols(2).AllowEditing = False
                    .Cols(2).Width = .Width() / 13.5  'SeqItem ID

                    .Cols(3).AllowEditing = False
                    .Cols(3).Width = .Width() / 4  'Item Name

                    .Cols(4).AllowEditing = False
                    .Cols(4).Width = .Width() / 6  'Batch NO

                    .Cols(5).AllowEditing = False
                    .Cols(5).Width = .Width() / 17  'Exp

                    .Cols(6).AllowEditing = False
                    .Cols(6).Width = .Width() / 7 'Location Code

                    .Cols(7).AllowEditing = False
                    .Cols(7).Width = .Width() / 14 'Availabele Stock

                    .Cols(8).AllowEditing = False
                    .Cols(8).Width = .Width() / 12 'Purchase Price
                    .Cols(8).Format = ("0.00")

                    .Cols(9).AllowEditing = False
                    .Cols(9).Width = .Width() / 7  'Amount
                    .Cols(9).Format = ("0.00")
                    'Rahul_ITS@20071120 : Add code
                    .Cols(10).DataType = System.Type.GetType("System.Int32")    'New Stock

                    .Cols(1).Visible = False
                    .Cols(11).Visible = False   ' Item ID

                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Setcontrol")
        End Try
    End Sub

    'Rahul_ITS@20071116: Add new code
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim IntCount As Integer
        Dim ItemLike As String
        Dim qtycount As Integer

        Dim NPrice As Double        'Rahul_ITS@20071117
        Dim NMargin As Double       'Rahul_ITS@20071117
        Dim OPrice As Double        'Rahul_ITS@20071117
        Dim OMargin As Double       'Rahul_ITS@20071117

        'For Stock Adjustment
        Try
            If _strStockType = "STKA" Then
                ' Stock ID          -----> .Item(intcount, 1)
                ' Batch NO          -----> .Item(intcount, 4)
                ' Availabele Stocke -----> .Item(intcount, 7)
                ' New Stock         -----> .Item(intcount, 10)
                ' Item ID           -----> .Item(intcount, 11)
                For IntCount = 1 To fgResults.Rows.Count - 1
                    With fgResults
                        If (.Item(IntCount, 10) <> "") Then
                            qtycount = .Item(IntCount, 10) - .Item(IntCount, 7)
                            BStock.SaveStockAdj(.Item(IntCount, 11), .Item(IntCount, 4), .Item(IntCount, 1), .Item(IntCount, 7), .Item(IntCount, 10), qtycount)
                        End If
                    End With
                Next

                'For Price Adjustment
            ElseIf _strStockType = "PRA" Then
                ' Stock ID       -----> .Item(intcount, 1)
                ' Batch NO       -----> .Item(intcount, 4)
                ' New Margin     -----> .Item(intcount, 12)
                ' New Sale Price -----> .Item(intcount, 13)
                ' Item ID        -----> .Item(intcount, 14)
                For IntCount = 1 To fgResults.Rows.Count - 1
                    With fgResults
                        If (.Item(IntCount, 12) <> "") Then
                            OMargin = BPurchaseInvoice.GetMargin(.Item(IntCount, 14))
                            OPrice = BStock.GetOldSalePrice(.Item(IntCount, 14))
                            NPrice = CDbl(.Item(IntCount, 13))
                            NMargin = CDbl(.Item(IntCount, 12))
                            BStock.SavePriceAdj(.Item(IntCount, 14), .Item(IntCount, 4), .Item(IntCount, 1), OMargin, OPrice, NMargin, NPrice)
                        End If
                    End With
                Next
            End If
            ItemLike = Trim(txtItemName.Text).ToString()
            SetCaption()

            fgResults.DataSource = BStock.GetStockDetails(SetData, _strStockType, ItemLike)

            Call Setcontrol()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
        End Try
    End Sub

    'Rahul_ITS@20071120 : Add code
    Private Sub fgResults_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgResults.AfterEdit

        Dim varsaleprice As Decimal
        Dim intcount As Integer
        Dim intValue As String
        Dim intPriceValue As String
        Try
            With fgResults
                If _strStockType = "PRA" Then
                    ' Purchase Price -----> .Item(intcount, 8)
                    ' New Margin     -----> .Item(intcount, 12)
                    ' New Sale Price -----> .Item(intcount, 13)
                    For intcount = 1 To fgResults.Rows.Count - 1
                        With fgResults
                            Select Case .Col
                                Case 12
                                    If (.Item(intcount, 12) <> "") Then
                                        intValue = .Item(intcount, 12)
                                        If IsNumeric(Trim(intValue)) = False Or Val(intValue) < 0 Then
                                            BCommon.ShowMsgError(10290)
                                            .Item(intcount, 12) = ""
                                            e.Cancel = True
                                        Else
                                            varsaleprice = Math.Round(((.Item(intcount, 12)) / 100) * .Item(intcount, 8), 2)
                                            .Item(intcount, 13) = .Item(intcount, 8) + varsaleprice
                                        End If
                                    End If
                                Case 13
                                    If (.Item(intcount, 13) <> "") Then
                                        intPriceValue = .Item(intcount, 13)
                                        If IsNumeric(Trim(intPriceValue)) = False Or Val(intPriceValue) < 0 Then
                                            BCommon.ShowMsgError(10290)
                                            .Item(intcount, 13) = ""
                                            e.Cancel = True
                                        Else
                                            If .Item(intcount, 8) > 0 Then
                                                varsaleprice = Math.Round(((.Item(intcount, 13) - .Item(intcount, 8)) / .Item(intcount, 8)) * 100, 2)
                                                .Item(intcount, 12) = varsaleprice
                                            Else
                                                .Item(intcount, 12) = 0
                                            End If
                                        End If
                                    End If
                            End Select
                        End With
                    Next

                ElseIf _strStockType = "STKA" Then
                    ' New Stock      -----> .Item(intcount, 10)
                    For intcount = 1 To fgResults.Rows.Count - 1
                        If (.Item(intcount, 10) <> "") Then
                            intValue = .Item(intcount, 10)
                            If IsNumeric(Trim(intValue)) = False Or Val(intValue) < 0 Then
                                BCommon.ShowMsgError(10290)
                                .Item(intcount, 10) = ""
                                e.Cancel = True
                                'ElseIf Val(.Item(intcount, 10)) Then
                                '    MessageBox.Show("test")
                            Else
                                If intValue.Contains(".") Then 'Restrict Decimal Value
                                    BCommon.ShowMsgError(10211)
                                    .Item(intcount, 10) = ""
                                    e.Cancel = True
                                End If
                            End If
                        End If
                    Next
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgResults_AfterEdit")
        End Try
    End Sub
    ' Divya@06/03/2008 
    Private Sub fgResults_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles fgResults.ValidateEdit
        Dim intMRP As Integer
        Dim varsaleprice As Decimal

        Try
            With fgResults
                If e.Col = 12 Then
                    If IsNumeric(Trim(sender.editor.text)) = False Or Val(sender.editor.text) < 0 Then
                        BCommon.ShowMsgError(10290)
                        sender.editor.text = ""
                        e.Cancel = True
                    ElseIf _strStockType = "PRA" Then
                        intMRP = BStock.GetMRP(.Item(e.Row, 14), .Item(e.Row, 4))
                        If sender.editor.text <> "" Or sender.editor.text <> Nothing Then
                            varsaleprice = .Item(e.Row, 8) + (Math.Round(((CDbl(sender.editor.text) / 100)) * .Item(e.Row, 8), 2))
                            If varsaleprice > intMRP Then
                                BCommon.ShowMsgError(10385)
                                sender.editor.text = ""
                                e.Cancel = True
                            End If
                        End If
                    End If
                    End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgResults_ValidateEdit")
        End Try
    End Sub

    Private Sub fgResults_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgResults.SetupEditor
        Try
            If _strStockType = "PRA" Then
                If e.Col = 12 Then
                    Dim ctl As Control = fgResults.Editor
                    CType(ctl, TextBox).MaxLength = 3
                End If
                If e.Col = 13 Then
                    Dim ctl As Control = fgResults.Editor
                    CType(ctl, TextBox).MaxLength = 5
                End If
            End If
            If _strStockType = "STKA" Then
                If e.Col = 10 Then
                    Dim ctl As Control = fgResults.Editor
                    CType(ctl, TextBox).MaxLength = 5
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgResults_SetupEditor")
        End Try
    End Sub

    Private Sub txtItemName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtItemName.KeyDown
        Try
            If e.Alt Then Exit Sub
            If e.KeyCode = Keys.Enter Then fgResults.Focus()

            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Exit Sub
                End If
            End If

            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtItemName_KeyDown")
        End Try
    End Sub
End Class